package com.salelogistics.sale.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.salelogistics.sale.domain.dto.DeliveryPlanDetailsDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.salelogistics.common.log.annotation.Log;
import com.salelogistics.common.log.enums.BusinessType;
import com.salelogistics.common.security.annotation.RequiresPermissions;
import com.salelogistics.sale.domain.SmsBatchPlan;
import com.salelogistics.sale.service.ISmsBatchPlanService;
import com.salelogistics.common.core.web.controller.BaseController;
import com.salelogistics.common.core.web.domain.AjaxResult;
import com.salelogistics.common.core.utils.poi.ExcelUtil;
import com.salelogistics.common.core.web.page.TableDataInfo;

/**
 * 计划分批Controller
 *
 * @author Hu Kangying
 * @date 2023-07-21
 */
@RestController
@RequestMapping("/batch")
public class SmsBatchPlanController extends BaseController {
    @Autowired
    @Qualifier("smsBatchPlanServiceImpl")
    private ISmsBatchPlanService batchPlanService;

    /**
     * 查询计划分批列表
     */
    @RequiresPermissions("sale:batch:list")
    @GetMapping("/list")
    public TableDataInfo list(SmsBatchPlan smsBatchPlan) {
        startPage();
        List<SmsBatchPlan> list = batchPlanService.selectSmsBatchPlanList(smsBatchPlan);
        return getDataTable(list);
    }

    /**
     * 查询月配送计划详细货物列表
     */
    @GetMapping("/d/{planId}")
    public TableDataInfo getListByPlanId(@PathVariable("planId") Long planId) {
        List<DeliveryPlanDetailsDto> list = batchPlanService.queryBatchPlanDetailsWithPlanId(planId);
        return getDataTable(list);
    }

    /**
     * 导出计划分批列表
     */
    @RequiresPermissions("sale:batch:export")
    @Log(title = "计划分批", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmsBatchPlan smsBatchPlan) {
        List<SmsBatchPlan> list = batchPlanService.selectSmsBatchPlanList(smsBatchPlan);
        ExcelUtil<SmsBatchPlan> util = new ExcelUtil<>(SmsBatchPlan.class);
        util.exportExcel(response, list, "计划分批数据");
    }

    /**
     * 获取计划分批详细信息
     */
    @RequiresPermissions("sale:batch:query")
    @GetMapping(value = "/{serialId}")
    public AjaxResult getInfo(@PathVariable("serialId") Long serialId) {
        return success(batchPlanService.selectSmsBatchPlanBySerialId(serialId));
    }

    /**
     * 新增计划分批
     */
    @RequiresPermissions("sale:batch:add")
    @Log(title = "计划分批", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmsBatchPlan smsBatchPlan) {
        return toAjax(batchPlanService.insertSmsBatchPlan(smsBatchPlan));
    }

    /**
     * 修改计划分批
     */
    @RequiresPermissions("sale:batch:edit")
    @Log(title = "计划分批", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmsBatchPlan smsBatchPlan) {
        return toAjax(batchPlanService.updateSmsBatchPlan(smsBatchPlan));
    }

    /**
     * 删除计划分批
     */
    @RequiresPermissions("sale:batch:remove")
    @Log(title = "计划分批", businessType = BusinessType.DELETE)
    @DeleteMapping("/{serialIds}")
    public AjaxResult remove(@PathVariable Long[] serialIds) {
        return toAjax(batchPlanService.deleteSmsBatchPlanBySerialIds(serialIds));
    }

    /**
     * 审核月计划
     */
    @PutMapping(value = "/process")
    public AjaxResult processPlan(@RequestBody Long planId) {
        logger.info("审核批次计划单，传入的计划id为：" + planId);
        return success(batchPlanService.processBatchPlan(planId, true));
    }
}
